package com.gt.sql

import com.gt.SCUtil
import org.apache.spark.sql.functions._
import org.apache.spark.sql.{DataFrame, Dataset, SparkSession}

/**
 * DSL 选择指定的列
 * 首先 -> 引入 import spark.implicits._
 *
 */
object SparkDataFrame_03_UDF {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SCUtil.buildLocalSparkSession()
    import spark.implicits._

    //val df: DataFrame = spark.read.json("data/user.json")
    val df: DataFrame = List(("a", 1), ("b", 2)).toDF("name", "age")
    val ds: Dataset[User] = df.as[User]

    spark.udf.register("aliasName",(name :String) => "xx-"+name)

    df.createOrReplaceTempView("user")
    spark.sql("select aliasName(name), age from user").show()




    spark.close()
  }


}
